﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using ICeFramework.Utilities.CommandLine;

namespace SharpGen
{
    static class Program
    {
        //public static MainForm mainForm;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        //[STAThread]
        //static void Main()
        //{
        //    Application.EnableVisualStyles();
        //    Application.SetCompatibleTextRenderingDefault(false);
        //    Application.Run(MainForm.Instance);
        //}

        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string[] arguments = args;

            if (arguments.Length == 1)
            {
                arguments = CommandLineHandler.CommandLineToArgs(string.Format("{0} {1}", Assembly.GetExecutingAssembly().Location, arguments[0]));
            }

            string projectFile = string.Empty;
            string outPath = string.Empty;
            string outNS = string.Empty;
            string debugFile = string.Empty;

            int i = 0;
            while (i < arguments.Length)
            {
                string sw = arguments[i].Replace("/", "");
                switch (sw.ToLower())
                {
                    case "t":
                        if (i + 1 >= arguments.Length)
                        {
                            MessageBox.Show("You must specify a template project file !", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        projectFile = arguments[i + 1];
                        if (!File.Exists(projectFile))
                        {
                            MessageBox.Show("You must specify a valid template project file !", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        i++;
                        break;
                    case "out":
                        if (i + 1 >= arguments.Length)
                        {
                            MessageBox.Show("You must specify an output path !", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        outPath = arguments[i + 1];
                        try
                        {
                            Directory.CreateDirectory(outPath);
                            if (!File.Exists(projectFile))
                            {
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(string.Format("Error while inspecting output path: {0} !", ex.Message), "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        i++;
                        break;
                    case "debug":
                        if (i + 1 >= arguments.Length)
                        {
                            MessageBox.Show("You must specify a debug file !", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        debugFile = arguments[i + 1];
                        if (!File.Exists(debugFile))
                        {
                            MessageBox.Show("You must specify a valid debug file !", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        i++;
                        break;
                    case "ns":
                        if (i + 1 >= arguments.Length)
                        {
                            MessageBox.Show("You must specify a namespace !", "Abort", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        outNS = arguments[i + 1].Replace(" ", "");
                        i++;
                        break;
                    default:
                        break;
                }
                i++;
            }


            if (string.IsNullOrEmpty(outPath))
            {
                outPath = @"C:\CodeGen\Generated";
            }
            if (string.IsNullOrEmpty(outNS))
            {
                outNS = "Business.Common";
            }

            MainForm.Instance.OutputPath = outPath;
            MainForm.Instance.OutputNamespace = outNS;
            MainForm.Instance.DebugFile = debugFile;

            MainForm.Instance.ProjectFile = projectFile;
            Application.Run(MainForm.Instance);
        }
    }
}
